﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Ornithopter.FlashDiskHelper
{
    public class FullScreenDetector
    {
        public FullScreenDetector Instance = new FullScreenDetector();

        [StructLayout(LayoutKind.Sequential)] 
        public struct RECT 
        {    
            public int Left;    
            public int Top;    
            public int Right;    
            public int Bottom; 
        } 
         
        //取得前台窗口句柄函数 
        [DllImport("user32.dll")] 
        private static extern IntPtr GetForegroundWindow(); 
        //取得桌面窗口句柄函数 
        [DllImport("user32.dll")] 
        private static extern IntPtr GetDesktopWindow(); 
        //取得Shell窗口句柄函数 
        [DllImport("user32.dll")] 
        private static extern IntPtr GetShellWindow(); 
        //取得窗口大小函数 
        [DllImport("user32.dll", SetLastError = true)] 
        private static extern int GetWindowRect(IntPtr hwnd, out RECT rc); 
         
        //桌面窗口句柄 
        private IntPtr desktopHandle; //Window handle for the desktop  
        //Shell窗口句柄 
        private IntPtr shellHandle; //Window handle for the shell  
        //因为桌面窗口和Shell窗口也是全屏，要排除在其他全屏程序之外。 

        public bool IsFullScreen()
        {
              //取得桌面和Shell窗口句柄 
            desktopHandle = GetDesktopWindow(); 
            shellHandle = GetShellWindow(); 
 
            //取得前台窗口句柄并判断是否全屏 
            bool runningFullScreen = false;  
            RECT appBounds; 
            Rectangle screenBounds; 
            IntPtr hWnd; 
            //取得前台窗口 
            hWnd = GetForegroundWindow(); 
            if (hWnd != null && !hWnd.Equals(IntPtr.Zero)) 
            { 
                //判断是否桌面或shell        
                if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle))) 
                { 
                    //取得窗口大小 
                    GetWindowRect(hWnd, out appBounds); 
                    //判断是否全屏 
                    screenBounds = Screen.FromHandle(hWnd).Bounds; 
                    if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width) 
                        runningFullScreen = true; 
                } 
            }
            return runningFullScreen;
        }
    }
}
